<!-- 快递单合单 -->
<template>
	<view>
		<scan></scan>
		<scanner></scanner>
		<sm></sm>
		<honeywell></honeywell>
		<u-sticky >
			<u-search placeholder="请输入快递单编码" :clearabled="true" v-model="goodsNumber" border-color="#606266" @custom="getcourierWaybill" ></u-search>
		</u-sticky>
		
		 <u-empty text="快递单列表为空,请扫描快递单" mode="list"  v-if="dataList.length <1"></u-empty>

		<u-card padding="16" v-for="(item,index) in dataList" :key="index" @click="clickCard(item)" >
			<view class="" slot="head" @longpress="longpressCard(item)">
				<view class="wrap">
					<u-row>
						<u-col span="12" text-align="left">
							<view style="background-color:aliceblue;">
<!-- 								 // <u-tag :text="item.orderOutNumber" type="success" size="large"  shape="circle"></u-tag> -->
								 <span style="font-size: 40rpx; color:chocolate;">{{item.orderOutNumber}}</span>
							</view>
						</u-col>
					</u-row>	
				</view>
			</view>
			
		</u-card>
		<u-row>
			<u-col span="12" text-align="center">
				<view class="weikaishi6"  >
					<u-button type="primary" v-if="this.dataList.length > 0 " @click="hd()">合单</u-button>
					
				</view>
			</u-col>
			
		</u-row>
		<view>
			<u-calendar v-if="false"  v-model="show" mode="date" max-date="2070-01-01" @change="change" ></u-calendar>
		</view>
		<view>
				<u-modal v-model="showModal" :content="content" @confirm="confirm1" @cancel="cancel" :show-cancel-button="showCancelButton"	 confirm-text="确认" cancel-text="取消">
					
				</u-modal>
		</view>
		
		<view>
				<u-modal v-model="showHDModal" :content="hDContent" @confirm="confirm" @cancel="cancel" :show-cancel-button="showCancelButton"	 confirm-text="确认合单" cancel-text="取消">
					
				</u-modal>
		</view>
		<!-- <u-popup v-model="popupShow" mode="center" closeable width="660rpx" height="720rpx" border-radius="10">
			<view class="u-text-center u-m-t-20 weikaishi3"><u-icon name="order" size="30" labelSize="15px" labelColor="#000000" label="拣货明细"></u-icon></view>
			<u-card padding="16" v-for="(item,index) in dataList2" :key="index">
				<view class="" slot="head">
					<view class="wrap">
						<u-row>
							<u-col span="12">
								<view class="weikaishi3 lanse">
									<span>{{item.storageGoodsname}},{{item.goodsNo}}</span>
								</view>
							</u-col>
						</u-row>
					</view>
				</view>
				<view class="" slot="body">
					<view class="wrap">
						<u-row>
							<u-col span="5" text-align="center">
								<view class="weikaishi4 u-line-1 u-m-t-10">
									<span>批次</span>
								</view>
							</u-col>
							<u-col span="3" text-align="center">
								<view class="weikaishi4 u-line-1 u-m-t-10">
									<span>应拣</span>
								</view>
							</u-col>
							<u-col span="4" text-align="center">
								<view class="weikaishi4 u-line-1 u-m-t-10">
									<span>实拣</span>
								</view>
							</u-col>
						</u-row>
						<u-row v-for="(item1,index1) in item.list" :key="item1.id">
							<u-col span="5" text-align="center">
								<view class=" u-m-t-10 weikaishi4 huanghang">
									<span>{{item1.storageBatchnumber}}</span>
								</view>
							</u-col>
							<u-col span="3" text-align="center" v-if="item1.storageNumber == item1.realNum">
								<view class=" u-m-t-10 weikaishi4 yiwancheng">
									<span>{{item1.storageNumber}}</span>
								</view>
							</u-col>
							<u-col span="3" text-align="center" v-else>
								<view class=" u-m-t-10 weikaishi4">
									<span>{{item1.storageNumber}}</span>
								</view>
							</u-col>
							<u-col span="4" text-align="center" v-if="item1.storageNumber == item1.realNum">
								<view class=" u-m-t-10 weikaishi4 yiwancheng">
									<span>{{item1.realNum}}</span>
								</view>
							</u-col>
							<u-col span="4" text-align="center" v-else>
								<view class=" u-m-t-10 weikaishi4">
									<span>{{item1.realNum}}</span>
								</view>
							</u-col>
						</u-row>
					</view>
				</view>
			</u-card>
		</u-popup> -->
		<view>
			<u-toast ref="uToast" />
		</view>
		
	</view>
</template>
<script>
	import scan from "@/components/uni-scan/scan.vue"
	import scanner from "@/components/uni-scan/scanner.vue"
	import sm from "@/components/uni-scan/sm.vue"
	import honeywell from "@/components/uni-scan/honeywell.vue"
	export default {
		components: {
			scanner,
			scan,
			sm,
			honeywell
		},
		data() {
			return {
				setMentiontime:'',
				dataList: [],
				dataList2: [],
				show:false,
				popupShow:false,
				areanumberlist:[],
				goodsNumber:'',
				pageNo:1,
				hasMore: true,
				keywordflag : true,
				content:'确定要移除该快递单吗？',
				hDContent:'确定要将快递单合单拣选吗？',
				showModal:false,
				showHDModal:false,
				selectItem :{},
				showCancelButton:true,
				list: [{
						cate_name: '待提交'
					}, {
						cate_name: '待审核'
					}, {
						cate_name: '待付款'
					}],
				current: 0,
				orderState:'待提交',
				scanOrderListStr:''

			}
		},
		onLoad(open) {
			//#ifdef H5 || APP-PLUS
			this.flex = 'u-flex'
			//#endif
			// this.refresh()
		},
		onHide() {
		  // 移除所有全局事件监听器
		  uni.$off('scan');
		  uni.$off('scanner');
		  uni.$off('sm');
		  uni.$off('honeywell');
		},
		onShow() {
			uni.$off('scan');
			uni.$on('scan', (res) => {
				console.log('扫码结果1：', res.codeStr);
				if(this.keywordflag){
					this.keyword = res.codeStr;
					this.getcourierWaybill(this.keyword);
					// this.getPackReviewByTrackNumber(this.keyword);
					// this.keywordflag = false
				}else{
					this.goodsNumber = res.codeStr
					this.getcourierWaybill(this.goodsNumber)
				}
			});
		
			uni.$off('scanner') // 每次进来先 移除全局自定义事件监听器
			uni.$on('scanner', (res) => {
				console.log('扫码结果2：', res.code);
				console.log(this.keywordflag)
				if(this.keywordflag){
					this.keyword = res.code;
					this.getcourierWaybill(this.keyword);
					// this.keywordflag = false
				}else{
					this.goodsNumber = res.code
					this.getcourierWaybill(this.goodsNumber)
				}
			})
			uni.$off('sm') // 每次进来先 移除全局自定义事件监听器
			uni.$on('sm', (res) => {
				console.log('扫码结果3：', res.code);
				if(this.keywordflag){
					this.keyword = res.code;
					this.getcourierWaybill(this.keyword);
					// this.getPackReviewByTrackNumber(this.keyword);
					// this.keywordflag = false
				}else{
					this.goodsNumber = res.code
					this.getcourierWaybill(this.goodsNumber)
				}
			})
			uni.$off('honeywell') // 每次进来先 移除全局自定义事件监听器
			uni.$on('honeywell', (res) => {
				console.log('扫码结果4：', res.code);
				if(this.keywordflag){
					this.keyword = res.code;
					// this.getPackReviewByTrackNumber(this.keyword);
					this.getcourierWaybill(this.keyword);
					// this.keywordflag = false
				}else{
					this.goodsNumber = res.code
					this.getcourierWaybill(this.goodsNumber)
				}
			})
		},
		filters:{
			formatDate(value) {
			      if (!value) return ''; // 如果没有值，返回空字符串
			      const date = new Date(value); // 将日期字符串转换为 Date 对象
			      const year = date.getFullYear(); // 获取年份
			      const month = String(date.getMonth() + 1).padStart(2, '0'); // 获取月份，+1 是因为月份从 0 开始
			      const day = String(date.getDate()).padStart(2, '0'); // 获取日期
			      return `${year}-${month}-${day}`; // 返回格式化的日期字符串
			    }

		},
		
		onPullDownRefresh() {
			this.refresh()
			uni.stopPullDownRefresh()
		},
		
		onPullDownRefresh() {
			this.refresh()
			uni.stopPullDownRefresh()
		},
		onReachBottom() {
			uni.showLoading({
				title: '加载中'
			})
			if (this.hasMore) {
				this.pageNo++ 
				setTimeout(() => {
					this.getBhList()
					uni.hideLoading()
				}, 500)
			} else {
				setTimeout(() => {
					uni.hideLoading()
					uni.showToast({
						icon: 'none',
						title: '已经到低了哦',
						duration: 2000
					});
				}, 500)
			}
		},
		
		methods: {
			//补货
			// toBh(item){
			// 	console.log(item)
			// 	this.$Router.push({
			// 		name: 'replenishGoods',
			// 		params: {
			// 			id: item.id
			// 		},
			// 	});
			// },
			confirm1(){
				console.log(this.selectItem)
				const index = this.dataList.findIndex(item => item === this.selectItem);
				if (index !== -1) {
				    this.dataList.splice(index, 1);
				}
				const orderOutNumber =  this.selectItem.orderOutNumber;
				this.scanOrderListStr.replace(orderOutNumber,'')
				console.log(this.dataList)
				console.log(this.scanOrderListStr)
			},
			//确认合单
			confirm(){
				console.log(this.dataList)
				console.log("确认合单")	
				console.log(this.scanOrderListStr)
				
				
				let scanOrderListStr = ''
				this.dataList.forEach(item => {
				
					//根据快递单查询当前拣货位库存
					scanOrderListStr  = scanOrderListStr + ','+ item.orderOutNumber
				});
				this.$http.post('/rest/storageStock/confirmMerge?expressDelivery='+scanOrderListStr,{
				},{
					params: {
						
					},
					header: {
						'Authorization' :uni.getStorageSync('token'),
					}
				}).then(res => {
					console.log(res)
					if(res.data.code == 500){
						// var item = {}
						// item.orderOutNumber = value
						// this.dataList.pop()
						const rightIndex = this.scanOrderListStr.lastIndexOf(',')
						if (rightIndex !== -1) {
						    this.scanOrderListStr = this.scanOrderListStr.substring(0, rightIndex);
						  }
						uni.showToast({
							title: res.data.message,
							icon: 'none'
						})
					}else{
						uni.showToast({
							title: '合单成功',
							icon: 'none'
						})
						this.$Router.replace({
							name: 'courierWaybillOut'
						});
					}
				})
			},
			
			
			//合单弹出确认
			hd(){
				this.showHDModal = true
			},
			//扫描快递单
			getcourierWaybill(value){
				debugger
				console.log(this.dataList)
				console.log(value)
				console.log(this.dataList.includes(value))
				console.log(this.scanOrderListStr)
				const scanOrders =  this.scanOrderListStr.slice(',');
				
				let count = 0 
				this.dataList.forEach(item => {
					if(item.orderOutNumber == value){
						
						count ++
					}
				});
				
				if(count >0){
					uni.showToast({
						title: value+'重复扫描',
						icon: 'none'
					})
				}else{
					this.getInventoryByCourierWaybill(value)
				}
				if(this.dataList.length ==0){
					this.scanOrderListStr  = this.scanOrderListStr + ','+ value
					this.getInventoryByCourierWaybill(value)
				}
				
				// if(this.dataList.includes(value)){
				// 	uni.showToast({
				// 		title: value+'重复扫描',
				// 		icon: 'none'
				// 	})
				// }else{
				// 	//根据快递单查询当前拣货位库存
				// 	this.scanOrderListStr  = this.scanOrderListStr + ','+ value
				// 	this.getInventoryByCourierWaybill(value)
				// }
			},
			//根据快递单查询当前拣货位库存
			
			getInventoryByCourierWaybill(value){
				console.log("根据快递单查询当前拣货位库存")
				console.log(value)
				var item = {}
				item.orderOutNumber = value
				this.dataList.push(item)
			
				let scanOrderListStr = ''
				this.dataList.forEach(item => {
				
					//根据快递单查询当前拣货位库存
					scanOrderListStr  = scanOrderListStr + ','+ item.orderOutNumber
				});
				this.$http.post('/rest/storageStock/findStorageStockByGoodsNumber?expressDelivery='+scanOrderListStr,{
				},{
					params: {
						lastDelivery:item.orderOutNumber
					},
					header: {
						'Authorization' :uni.getStorageSync('token'),
					}
				}).then(res => {
					console.log(res)
					if(res.data.code == 500){
						// var item = {}
						// item.orderOutNumber = value
						this.dataList.pop()
						const rightIndex = this.scanOrderListStr.lastIndexOf(',')
						if (rightIndex !== -1) {
						    this.scanOrderListStr = this.scanOrderListStr.substring(0, rightIndex);
						  }
						uni.showToast({
							title: res.data.message,
							icon: 'none'
						})
					}else{
						// var item = {}
						// item.orderOutNumber = value
						// this.dataList.push(item)
					}
				})
			},
			longpressCard(item1){
				
				console.log(item1)	
				this.selectItem = item1
				this.showModal = true
				
							
				// const index = this.goodsList.findIndex(item => item === item1);
				// if (index !== -1) {
				//     this.goodsList.splice(index, 1);
				// }
				// console.log(this.goodsList)
			},
			cancel(){
				
			},
			clickCard(item){
				console.log(item)
				this.$Router.replace({
					name: 'mdBhDetails',
					params: item,
				});
			},
			changeType(index) {
				this.current = index;
				console.log(this.current)
				if(this.current == 0){
					this.orderState = '待提交'
				}else if(this.current == 1){
					this.orderState = '待审核'
				}else if(this.current == 2){
					this.orderState = '待支付'
				}
				this.refresh();
			},
			getBHByGoodsNumber(value){
				 console.log(123)
				 console.log(value)
				 let dataList1 = [];
				for(let item of this.dataList) {
					if(item.goodsNumber.includes(value)){
						dataList1.push(item)
					}
				}
				this.dataList = dataList1
				if(value == null || value == '' ){
					this.refresh()
				}
			},
			checkboxChange(e){
				console.log(e)
			},
			refresh(){
				this.pageNo = 1
				this.hasMore = true
				this.dataList = [];
				uni.showLoading({
					title: '加载中...'
				})
				this.getBhList()
				uni.hideLoading()
			},
			
			
			getBhList(){
				this.$http.post('/rest/mdbh/mdbhList',{
				},{
					params: {
						trackNumber: this.keyword,
						orderState:this.orderState,
						page: this.pageNo,
						limit: 5
					},
					header: {
						'Authorization' :uni.getStorageSync('token'),
					}
				}).then(res => {
					console.log(res)
					if (this.pageNo == 1) {
						this.dataList = res.data.data
					} else {
						this.dataList = this.dataList.concat(res.data.data)
					}
					// this.orderCount = res.data.count
					this.hasMore = this.pageNo * 5 < res.data.count
					
				})
			},
		},
	}
</script>
<style lang="scss">
	.huanhang{
		word-break:break-all;
	}
</style>